// pages/smjs/smjs.js
var util = require('../../utils/util.js')
const DB = wx.cloud.database()
//获取应用实例
const app = getApp()
let id = ""

Page({

  /**
   * 页面的初始数据
   */
  data: {
    //用户身份信息
    openid: wx.getStorageSync('openid'),
    nickname: "",
    userInfo: {},
    hasUserInfo: false,
    register: 0,
    canIUse: 0,
    //导航栏处理 
    rpxdata: wx.getStorageSync('rpxdata'),
    navigationBarAndStatusBarHeight: wx.getStorageSync('statusBarHeight') + wx.getStorageSync('navigationBarHeight') + 'px',
    navbarData: {
      showCapsule: 0,
      title: "扫码借还书"
    },
    //处理按键颜色
    btn_js: 0,
    btn_hs: 0,

    jyren: "刘春红"

  },

  //借书处理
  jieshu: function () {
    this.setData({
      btn_js: 1
    })
    let that = this
   
    if (that.data.nickname == '' || that.data.opinid == '') {
      wx.showToast({
        title: '请开通权限',
        image: '/images/fail.png',
        duration: 2000
      })
      that.setData({
        btn_js: 0
      })
    } else {
      if (that.data.register == 1) {
        wx.scanCode({
          onlyFromCamera: false,
          success: (res) => {
            let isbn = res.result
            DB.collection("lchbook").where({
              isbn: isbn
            }).get({
              success: (res) => {
                let id = res.data[0]._id
                let stock = res.data[0].stock
                if (stock == 0) {
                  wx.showToast({
                    title: '书被人借走了',
                    duration: 2000,
                    image: '/images/fail.png'
                  })
                  that.setData({
                    btn_js: 0
                  })
                } else {
                  DB.collection("lchbook").doc(id).update({
                    data: {
                      jyren: that.data.nickname,
                      openid: that.data.openid,
                      jystate: true,
                      jytime: util.formatTime(new Date()),
                      jydate: util.formatDate(new Date()),
                      stock: stock - 1
                    },
                    success: (res) => {
                      wx.showToast({
                        title: '借书成功',
                        duration: 2000,
                        icon: 'success'
                      })
                      that.setData({
                        btn_js: 0
                      })
                    },
                    fail: (res) => {
                      wx.showToast({
                        title: '数据库借书失败',
                        duration: 2000
                      })
                      that.setData({
                        btn_js: 0
                      })
                    }
                  })
                }
              }
            })
          },
          fail: (res) => {
            wx.showToast({
              title: '扫码失败',
              duration: 2000,
              image: '/images/fail.png'
            })
            that.setData({
              btn_js: 0
            })
          }
        })
      } else {
        wx.showToast({
          title: '未审核通过',
          image: '/images/fail.png',
          duration: 2000
        })
      }
    }
  },
  //还书处理
  huanshu() {
    this.setData({
      btn_hs: 1
    })
    let that = this
    if (that.data.nickname == '' || that.data.opinid == '') {
      wx.showToast({
        title: '请开通权限',
        image: '/images/fail.png',
        duration: 2000
      })
      that.setData({
        btn_hs: 0
      })
    } else {
      if (that.data.register == 0) {
        wx.showToast({
          title: '未审核通过',
          image: '/images/fail.png',
          duration: 2000
        })
        that.setData({
          btn_hs: 0
        })
      } else {
        wx.scanCode({
          onlyFromCamera: false,
          success: (res) => {
            let isbn = res.result
            DB.collection("lchbook").where({
              isbn: isbn
            }).get({
              success: res => {
                let id = res.data[0]._id
                let stock = res.data[0].stock
                let jynum = res.data[0].jynum
                if (stock == 1) {
                  wx.showToast({
                    title: '已有库存',
                    image: '/images/fail.png',
                    duration: 2000
                  })
                } else {
                  DB.collection("lchbook").doc(id).update({
                    data: {
                      hsstate: true
                    },
                    success: function () {
                      wx.showToast({
                        title: '待还成功',
                        duration: 2000,
                        icon: 'success'
                      })
                      that.setData({
                        btn_hs: 0
                      })
                    },
                    fail: (res) => {
                      wx.showToast({
                        title: '数据库失败 ',
                        duration: 2000,
                        image: '/images/fail.png'
                      })
                      that.setData({
                        btn_hs: 0
                      })
                    }
                  })
                }
              },
              fail: (res) => {
                wx.showToast({
                  title: '未找到本书',
                  duration: 2000,
                  image: '/images/fail.png'
                })
                that.setData({
                  btn_hs: 0
                })
              }
            })
          },
          fail: (res) => {
            wx.showToast({
              title: '扫码失败，请在管理里还书',
              duration: 2000
            })
            that.setData({
              btn_hs: 0
            })
          },
        })
      }
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //读取用户注册信息
    this.setData({openid: wx.getStorageSync('openid')})

    //tabbar显示
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        tabIndex: wx.getStorageSync('tabIndex')
      })
    }

    //获取头像相关信息，是否显示按钮 
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true,
        nickname: app.globalData.userInfo.nickName
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true,
          nickname: app.globalData.userInfo.nickName
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true,
            nickname: res.userInfo.nickName
          })
        }
      })
    }


    DB.collection("lchuser").where({
      openid: this.data.openid
    }).get().then(
      res => {
        this.setData({register:res.data[0].register})}
    )
    


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }

})